<?php
class lib_company_fankui extends lib_base{
	public $table_name = "qc_company_fankui";
    public $pk = "id";

    public function getList($cond,$h5=0){

        if($h5==1){
            $result = $this->get_loaddata($cond,'up_time desc,id desc','*',10);
        }else{
            $result = $this->get_pagelist($cond,'up_time desc,id desc','*',20);
        }

        if($result){
            $lists = $result['list'];

            foreach ($lists as $val) {

                if($val['admin_ids']){
                    $val['admin_ids'] = explode(',',$val['admin_ids']);
                }
                $list[] = $val;
            }


            $data['list'] = $list;
            $data['showPage'] = $result['showPage'];
            $data['total'] = $result['total'];

            return $data;
        }else{
            return false;
        }

    }


    public function add($data)
    {
        $data['status'] = 1;
        $data['add_time'] = date('Y-m-d H:i:s');
        if($data['setting']){
            $data['setting'] = serialize($data['setting']);
        }
        return $this->create($data);
    }

    public function get_device($id)
    {
        $info = $this->findById($id);
        $info['setting'] = unserialize($info['setting']);
        return $info;
    }

    public function get_detail($id)
    {
        $info = $this->findById($id);
        if($info){
            if($info['shifu_id']){
                $info['shifu'] = D('company_admin')->findById($info['shifu_id']);
            }
			if($info['user_id']){
				$info['user']= D('company_user')->findById($info['user_id']);
			}
            // $info['setting'] = unserialize($info['setting']);


            if($info['area_id']){
                $info['area'] = D('company_area')->get_info($info['area_id']);
            }
            if($info['cate_id']){
                $info['cate'] = D('company_cate')->get_info($info['cate_id']);
            }
            if($info['zone_id']){
                $info['zone'] = D('company_zone')->get_info($info['zone_id']);
            }
            if($info['bingchuang_id']){
                $info['bingchuang'] = D('company_bingchuang')->get_info($info['bingchuang_id']);
            }
			if($info['diaocha_id']){
				$info['diaocha']= D('company_diaocha')->get_info($info['diaocha_id']);
			}

            if($info['admin_ids']){
				$info['admin_ids']= explode(',',$info['admin_ids']);
			}


            if($info['photo']){
                $photos_arr = explode(',', $info['photo']);
                foreach($photos_arr as $v){
                    $info['photos'][] = D('upload_file')->get_image_url($v);
                }
            }

            $cond = "status=1 and fankui_id={$info['id']}";
            $logs = D('company_fankui_log')->get_list($cond);
            if($logs){
                $i = 1;
                foreach ($logs as $key => $val) {
                    if($val['intro']){
                        $val['intro'] = unserialize($val['intro']);
                        if($val['intro']['photo']){
                            $photos_arr = explode(',', $val['intro']['photo']);
                            foreach($photos_arr as $v){
                                $val['intro']['photos'][] = D('upload_file')->get_image_url($v);
                            }
                        }

                    }
                    $val['i'] = $i;

                    $list[] = $val;

                    $i = $i + 1;
                }

                $info['logs'] = $list;
                $info['logs_maxid'] = min(array_keys($list));
            }
            return $info;
        }
    }

    public function order_act($order_id,$data,$order_status=0){

        $order = D('company_fankui')->get_info($order_id);
        if($order){

            if($data['order_status'] == 99){
                $arrBase['shifu_id'] = 0;
                $arrBase['admin_ids'] = '';
                $arrBase['order_status'] = 1;
            }else{
                $arrBase['shifu_id'] = $data['shifu_id'];
                $arrBase['order_status'] = $data['order_status'];
            }
            $arrBase['up_time'] = date('Y-m-d H:i:s');

            return $this->edit($order_id,$arrBase);
        }

    }


    //二维码
	public function qrcode($url,$info,$company,$dir2=''){
		header("Content-Type: text/html;charset=utf-8");
		// $id = arg('id');
		// $this->info = $this->findById($id);
		$zone = D('company_zone')->findById($info['zone_id']);

		// require(APP_DIR.'/protected/include/lib/phpqrcode.php');
		$value = $url;//二维码内容
		// $value = "/index/main/qrcode?device_id=".$id;//二维码内容

		$errorCorrectionLevel = 'L';//容错级别
		$matrixPointSize = 6;//生成图片大小
		$cnname = $company['name'].'<br />'.$info['name'];
		// $name2 = iconv('UTF-8', 'GB18030', $name);
        // $name = $this->zone['id'].'_'.$this->info['id'];
        $name = $info['id'];

		$dir = "./upload/company_{$company['id']}/";
        if($dir2){
            $dir .= "{$dir2}/";
        }
        if (!is_dir($dir)) {
            mkdir($dir, 0755, true);
        }

		//生成二维码图片
		$file = $dir.$name.'.png';
		$code = $dir.$name.'_code.png';
		QRcode::png($value, $file, $errorCorrectionLevel, $matrixPointSize, 6);

		if($company['logo']){
			$logo = './upload/'.$company['logo'];//准备好的logo图片
		}else{
			$logo = './upload/logo.png';
		}
		$QR = $file;//已经生成的原始二维码图

		if ($logo !== FALSE) {
		 $QR = imagecreatefromstring(file_get_contents($QR));
		 $logo = imagecreatefromstring(file_get_contents($logo));
		 $QR_width = imagesx($QR);//二维码图片宽度
		 $QR_height = imagesy($QR);//二维码图片高度
		 $logo_width = imagesx($logo);//logo图片宽度
		 $logo_height = imagesy($logo);//logo图片高度
		 $logo_qr_width = $QR_width / 5;
		 $scale = $logo_width/$logo_qr_width;
		 $logo_qr_height = $logo_height/$scale;
		 $from_width = ($QR_width - $logo_qr_width) / 2;
		 // $logo_qr_height = $logo_qr_height + 110;
		 // dump($logo_qr_width);exit;
		 //重新组合图片并调整大小
		 imagecopyresampled($QR, $logo, $from_width, $from_height, 0, 0, $logo_qr_width,
		 $logo_qr_height, $logo_width, $logo_height);
		}


		$text = $company['name']."--".$zone['name']."--".$info['name']."({$info['id']})";
		$this->mark_photo($file,$text,$file);


		$file = str_replace('./', '', $file);

		//在图片中添加文字拼接图片
		//输出图片
		// Header("Content-type: image/png");
		// ImagePng($QR,$code);

		// echo $cnname.'<br />';
		// echo '<img src="/'.$file.'"  alt="" />';
        return $file;

    }

    public function mark_photo($background,$text,$filename){

		$image = imagecreatefrompng($background);
		$font = './static/app/fonts/msyh.TTF';
		$color = imagecolorallocate($image,0,0,0);
		imagettftext($image, 10, 0, 25, 25, $color, $font, $text);
		// header("Content-Type:image/png");
		ImagePng($image, $filename);
		imagedestroy($image);
	}





    public function send_weixin_msg($accessToken,$shifu,$fk_id,$keyword1)
    {

		$appid = 'wx913120c9717ff5d3';
    	$appsecret = '8a284b6f7fbeaa91007f036363eae369';
		$templateId = "CQ_3g4-qYAbB9_QiMniv5TfkAi0vw0pORCZtcf__YdU";

        //$accessToken = D('weixin')->getAccessToken($appid, $appsecret);

        $templateId = "CQ_3g4-qYAbB9_QiMniv5TfkAi0vw0pORCZtcf__YdU";
        // 师傅发送模版消息
        $user = D('company_user')->findById($shifu['user_id']);
        if($user){
            $sys_user = D('sys_user')->findById($user['wxuser_id']);
            if($sys_user){
                // 发送模版消息
                $messdata = array(
                    'accessToken' =>$accessToken,
                    'openId' =>$sys_user['open_id'],
                    'templateId' =>$templateId,
                    'first' =>"FK-{$fk_id}",
                    'keyword1' =>"{$keyword1}",
                    'keyword2' =>date("Y-m-d H:i:s"),
                );
                $url = "https://t.20m.cn/index_admin/login/wxlogin?from=/index_admin/fankui/index?tab=2";
                $res = D('weixin')->sendWxMessage($messdata,$url);

                return true;
            }
        }
        return false;
    }


}